/**
 * 添加或者修改页面
 */
var ArchiveDestructionInfoDlg = {
    data: {
        xhdh: "",
        jbr: "",
        jysj: "",
        beiz: ""
    }
};

layui.use(['form', 'admin', 'ax','table','func'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;
    var func = layui.func;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        if (data.field.archiveInfoIds=='') {
            Feng.error("请选择档案");
            return false;
        }
        var ajax = new $ax(Feng.ctxPath + "/archiveDestruction/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/archiveDestruction'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


    //处理借阅列表
    var tableId = "destroy_archiveList";
    /**
     * 初始化表格的列
     */
    var initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'id'},
            {field: 'wjid',  title: '卷宗号'},
            {field: 'mc',    title: '档案名称'},
            {field: 'dalx',  title: '档案类型', templet: '#dalx'},
            {field: 'ssrrmc',  title: '所属人名称'},
            {field: 'mj',  title: '密级', templet: '#mj'},
            {field: 'ly',  title: '来源', templet: '#ly'},
            {fixed: 'right', width: 120, align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + tableId,
        page: false,
        data: [],
        height: "500",
        toolbar: '#toolbar',
        defaultToolbar: [],
        cellMinWidth: 100,
        cols: initColumn(),
    });


    table.on('tool('+tableId + ')',function(obj){
        if (obj.event == 'delete') {
            var operator = function(){
                obj.del();
            }
            Feng.confirm("确定要删除吗？", operator);
        }
    });

    // 工具条点击事件
    table.on('toolbar(' + tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'add') {
            //弹出框选择档案
            func.open({
                title: '请选择档案',
                height: 700,
                // width: 900,
                tableId: tableId,
                content: Feng.ctxPath + "/archiveInfo/dialog?url=/archiveInfo/list",
                endCallback: function (data) {
                    tableResult.reload({
                        data: (function() {
                            var cache = table.cache.destroy_archiveList;
                            var temp = cache;
                            for (var i in data) {
                                var item = data[i];
                                var isContain = false;
                                for (var j in temp) {
                                    var ori = temp[j];
                                    if (item.id == ori.id) {
                                        isContain = true;
                                        continue;
                                    }
                                }
                                if (!isContain) {
                                    temp.push(item);
                                }
                            }
                            var ids = [];
                            temp.forEach(function (val,index) {
                                ids.push(val.id);
                            })
                            $('#archiveInfoIds').val(ids.join(","));
                            return temp;
                        })(),
                    });
                }
            });

        }
    });


    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/archiveDestruction'
    });

});